<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\Admin\PayType;
use App\Http\Requests\Admin\PayTypeController\doRule;

class PayTypeController extends Controller
{
    public function index()
    {
        $result = [
            'result'=>PayType::lists(),
        ];

        return view('admin.pay_type.index', $result);
    }

    public function add()
    {
        $result = [

        ];

        return view('admin.pay_type.edit',$result);
    }

    public function edit(Request $request, $id)
    {
        $result = [
            'id'=>$id,
            'result'=> PayType::info($id),
        ];

        return view( 'admin.pay_type.edit', $result);
    }

    public function do(doRule $request)
    {
        if ($request->ajax()) {

            $input = $request->all();

            $image_avatar = !empty($request->file('pay_img')) ? $request->file('pay_img') : [];

            if (!empty($image_avatar)) {
                $path = $image_avatar->store('public/upload');
                $path = str_replace('public/', 'storage/', $path);
                $input['pay_img'] = $path;
            } else {
                unset($input['pay_img']);
            }

            if (empty($input['id'])) {
                unset($input['id']);
                PayType::add($input);
            } else {
                PayType::edit($input['id'],$input);
            }

            return self::success([]);
        }
    }

    public function del($id)
    {

        PayType::del($id);

        return self::success([]);

    }
}
